﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.Editors;
using Einstein.Data; 
using Einstein.Utility;
using DevComponents.DotNetBar;
using Einstein.Group.DAL;

namespace Einstein.Platform.Window.Basic
{
    public partial class Basic_Org_New : Office2007Form
    {
        public Basic_Org_New()
        {
            InitializeComponent();
            BindOrg();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {

            if (superValidator1.Validate())
            {
                DeptInfoDAL dal = new DeptInfoDAL();
                DeptInfo info = new DeptInfo { DeptName = txtName.Text, Desc = txtDesc.Text, ParentID = int.Parse(((ComboItem)ddlParent.SelectedItem).Value.ToString()) };
                bool isEdit = false;
                if (this.DeptInfo != null)
                {
                    info.DeptID = this.DeptInfo.DeptID;
                    isEdit = true;
                }
                if (dal.Exist(info))
                {
                    BoxManager.ShowWarning("已经存在相同的组织名称，请重试。");
                    return;
                }
                bool result = isEdit ? dal.Update(info) : dal.Add(info);
                if (result)
                {
                    BoxManager.ShowSuccess();
                }
                else
                {
                    BoxManager.ShowFail();
                }

                this.DialogResult = DialogResult.OK;
            }

        }

        void BindOrg()
        {
            IList<DeptInfo> list = new DeptInfoDAL().SelectAll();
            if (list != null)
            {
                foreach (DeptInfo item in list.Where(a => a.ParentID == 0))
                {
                    ddlParent.Items.Add(new ComboItem { Value = item.DeptID, Text = item.DeptName });
                    IList<DeptInfo> child = list.Where(a => a.ParentID == item.DeptID).ToList();
                    if (child != null)
                    {
                        AddTree(child, list, 1);
                    }
                }
            }

            ddlParent.Items.Insert(0, new ComboItem { Value = 0, Text = "无上级部门" });
        }

        void AddTree(IList<DeptInfo> childList, IList<DeptInfo> mainList, int level)
        {
            foreach (DeptInfo item in childList)
            {
                ddlParent.Items.Add(new ComboItem { Value = item.DeptID, Text = " ".PadLeft(level * 5) + item.DeptName });
                IList<DeptInfo> child = mainList.Where(a => a.ParentID == item.DeptID).ToList();
                if (child != null)
                {
                    AddTree(child, mainList, level + 1);
                }
            }
        }
        public DeptInfo DeptInfo
        {
            get;
            set;
        }


        public void BindData()
        {
            for (int i = 0; i < ddlParent.Items.Count; i++)
            {
                ComboItem item = ddlParent.Items[i] as ComboItem;
                if (item.Value.ToString() == DeptInfo.ParentID.ToString())
                {
                    ddlParent.SelectedIndex = i;
                    break;
                }

            }
            txtName.Text = DeptInfo.DeptName;
            txtDesc.Text = DeptInfo.Desc;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
